package com.spark.WordCount;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import scala.collection.LinearSeq;

import java.util.Arrays;
import java.util.List;

/**
 * 使用Java语言创建RDD 通过集合
 */
public class CreateRddByArrayJava {
    public static void main(String[] args) {
        SparkConf sparkConf=new SparkConf()
                .setAppName("WordCount")
                .setMaster("local");
        JavaSparkContext context=new JavaSparkContext(sparkConf);
        List<Integer> list= Arrays.asList(1,2,3,4,5,6);
        JavaRDD rdd=context.parallelize(list);
        //第一个和第二个Integer是输入的两个参数类型，第三个Integer是输出的数据类型
        Integer sum= (Integer) rdd.reduce(new Function2<Integer,Integer,Integer>() {
            @Override
            public Integer call(Integer l1, Integer l2) throws Exception {
                return l1+l2;
            }
        });
        System.out.println(sum);
        context.stop();
    }
}
